package com.netke.webservice;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class WebServiceUtil {
	public final static String SERVER_NS = "http://WebXml.com.cn/";
	public final static String SERVER_URL ="http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
	

	public static List<String> execute(String method,Map<String,String> params){
		HttpTransportSE ht = new HttpTransportSE(SERVER_URL);
		ht.debug = true;
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		SoapObject obj = new SoapObject(SERVER_NS, method);
		if(params!=null && params.size()>0){
			for(Map.Entry<String, String> entry:params.entrySet()){
				obj.addProperty(entry.getKey(), entry.getValue());
			}
		}
		envelope.bodyOut = obj;
		envelope.dotNet = true;
		ArrayList<String> list = new ArrayList<String>();
		try {
			ht.call(SERVER_NS+method, envelope);
			if(envelope.getResponse()!=null){
				SoapObject result =(SoapObject)envelope.bodyIn;
				SoapObject detail = (SoapObject)result.getProperty(method+"Result");
				
				for(int i=0;i<detail.getPropertyCount();i++){
					list.add(detail.getProperty(i).toString().split(",")[0]);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		return list;
	}
	public static SoapObject getWeather(String method,Map<String,String> params){
		HttpTransportSE ht = new HttpTransportSE(SERVER_URL);
		ht.debug = true;
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		SoapObject obj = new SoapObject(SERVER_NS, method);
		if(params!=null && params.size()>0){
			for(Map.Entry<String, String> entry:params.entrySet()){
				obj.addProperty(entry.getKey(), entry.getValue());
			}
		}
		envelope.bodyOut = obj;
		envelope.dotNet = true;
		SoapObject detail = null; ;
		try {
			ht.call(SERVER_NS+method, envelope);
			if(envelope.getResponse()!=null){
				SoapObject result =(SoapObject)envelope.bodyIn;
				detail = (SoapObject)result.getProperty(method+"Result");
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		return detail;
	}
	

}
